//
//  ZHWCycleView.m
//  06-CycleImages
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZHWCycleView.h"

@interface ZHWCycleView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *iamgesPage;
@end

@implementation ZHWCycleView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 子控件初始化时, frame不要与初始化传入的frame的一样
        // 推荐使用self.bounds / CGRectMake(0, 0, frame.size.width, frame.size.height)
        [self creatCycleScrollView];
        [self creatImagesPage:frame];
    }
    return self;
}

- (void)creatCycleScrollView {
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.delegate = self;
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_cycleScrollView];
    
}
// 后期需要添加page的位置改变, 这里默认位置
- (void)creatImagesPage:(CGRect)frame {
    _iamgesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
    _iamgesPage.numberOfPages = 5;
    _iamgesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - _iamgesPage.frame.size.height / 2);
    [self addSubview:_iamgesPage];
    [_iamgesPage addTarget:self action:@selector(pageIndexChanged:) forControlEvents:UIControlEventValueChanged];
}
// 自定义方法
- (void)setImagesWithArray:(NSArray *)array {
    // 根据传入的数组进行处理 生成图片数组
    NSArray *imagesArray = [self handleArray:array];
    // 逻辑判断时 需要用到数组
    self.imagesArray = imagesArray;
    // 根据图片数组生成对应的ImageView
    [self creatAllImageViewsWithImagesArray:_imagesArray];

    _iamgesPage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(self.bounds.size.width * imagesArray.count, 0);
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}

- (void)creatAllImageViewsWithImagesArray:(NSArray *)imagesArray {
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}

- (NSArray *)handleArray:(NSArray *)array {
    // 判断数组中元素类型
    id firstObject = [array firstObject];
    // 判断对象的类型 isKindOfClass
    NSMutableArray *imagesArray = [NSMutableArray array];
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组转成UIImage数组
        for (NSString *imageName in array) {
            [imagesArray addObject:[UIImage imageNamed:imageName]];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imagesArray setArray:array];
    }
    // 获取到第一张图
    UIImage *firstImage = [imagesArray firstObject];
    // 获取到最后一张图
    UIImage *lastImage = [imagesArray lastObject];
    // 下标为0的位置放最后一张图
    [imagesArray insertObject:lastImage atIndex:0];
    // 在数组最后添加第一张图
    [imagesArray addObject:firstImage];
    /* 可变数组变为不可变数组 */
    return [imagesArray copy];
}
// pageControl点击事件
- (void)pageIndexChanged:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}
#pragma mark - scrollView Delegate
// 减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 改变pageControl的对应下标
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _iamgesPage.currentPage = page - 1;
    if (page == 0) {
        // 改变滚动视图的偏移量
        scrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        _iamgesPage.currentPage = _imagesArray.count - 1 - 2;
    } else if (page == _imagesArray.count - 1) {
        scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _iamgesPage.currentPage = 0;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
